﻿<%@ Page Language="C#" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace=" System.Collections" %>
<%@ Import Namespace="System.Collections.Generic" %>
<%@ Import Namespace="TarreOA.TarreOAModels" %>
<%@ Import Namespace="TarreOA.TarreOABLL" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">

    private void DataList_DataTable(string txtStart, string txtEnd, string type)
    {
        User curLoginUser = (User)Session["user"];
        UserCalendarArrangetbService ucatbService = new UserCalendarArrangetbService();
        List<UserCalendarArrangetb> list = ucatbService.findAllBetweenStartAndEnd(txtStart, txtEnd, type);
        DataTable dt = new DataTable();
        dt.Columns.Add("CalendarArrangeStartTime", typeof(string));
        dt.Columns.Add("CalendarArrangeEndTime", typeof(string));
        dt.Columns.Add("CalendarArrangeId", typeof(string));
        dt.Columns.Add("CalendarArrangeTitle", typeof(string));
        dt.Columns.Add("CalendarArrangeContent", typeof(string));
        dt.Columns.Add("CalendarArrangeType", typeof(string));
        dt.Columns.Add("CalendarArrangeDate", typeof(string));
        dt.Columns.Add("CalendarArrangeState", typeof(string));
        if (list != null && list.Count > 0)
        {
            foreach (UserCalendarArrangetb ucatb in list)
            {
                DataRow dr = dt.NewRow();
                dr["CalendarArrangeStartTime"] = ucatb.CalendarArrangeStartTime;
                dr["CalendarArrangeEndTime"] = ucatb.CalendarArrangeEndTime;
                dr["CalendarArrangeId"] = ucatb.CalendarArrangeId.ToString();
                dr["CalendarArrangeTitle"] = ucatb.CalendarArrangeTitle;
                dr["CalendarArrangeContent"] = ucatb.CalendarArrangeContent;
                dr["CalendarArrangeType"] = ucatb.CalendarArrangeType.Equals("1") ? "工作事务" : "个人事务";
                dr["CalendarArrangeDate"] = ucatb.CalendarArrangeDate.ToShortDateString();
                string state = "";
                DateTime startTime = DateTime.Parse(ucatb.CalendarArrangeDate.ToShortDateString() + " " + ucatb.CalendarArrangeStartTime);
                DateTime endTime = DateTime.Parse(ucatb.CalendarArrangeDate.ToShortDateString() + " " + ucatb.CalendarArrangeEndTime);
                if (startTime < DateTime.Now && DateTime.Now < endTime)
                {
                    state = "<font color='green'><b>进行中</b></font>";
                }
                else if (endTime > DateTime.Now)
                {
                    state = "<font color='blue'><b>未至</b></font>";
                }
                else
                {
                    state = "<font color='red'><b>已过期</b></font>";
                }
                dr["CalendarArrangeState"] = state;
                string message = "";
                try
                {
                    if(ucatb == null)
                    {
                        message = "ucatb = null;"; 
                    }   else if(ucatb.CurUser == null)
                    {
                        message = "ucatb.CurUser = null";  
                    }  
                    else if(curLoginUser == null)
                    {
                        message = "curLoginUser = null"; 
                    } 
                    if(ucatb.CurUser.LoginName.Equals(curLoginUser.LoginName))
                    {
                        dt.Rows.Add(dr);
                    }
                    //StreamWriter sw = File.AppendText("d:\\log.txt");
                    //sw.WriteLine("Exception:");
                    //sw.Close();
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    //StreamWriter sw = File.AppendText("d:\\log.txt");
                    //sw.WriteLine(message + ex.Message);
                    //sw.Close();
                }
            }
            this.DataList1.DataSource = dt.DefaultView;
            this.DataList1.DataBind();
        }
        else
        {
            this.DataList1.DataSource = null;
            this.DataList1.DataBind();
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("ArrangeIndex.aspx");
    }
    
    protected void DataList1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        if (e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
        {
            string caid = ((HtmlInputHidden)e.Item.FindControl("Hidcaid")).Value.Trim();
            string c_dt = ((HtmlInputHidden)e.Item.FindControl("HidCalendarDate")).Value.Trim(); 
            ((LinkButton)e.Item.FindControl("LbtDisplay")).Attributes.Add("caid", caid);
            ((LinkButton)e.Item.FindControl("LbtDisplay")).Attributes.Add("c_dt", c_dt); 
            ((LinkButton)e.Item.FindControl("LbtEdit")).Attributes.Add("caid", caid);
            ((LinkButton)e.Item.FindControl("LbtEdit")).Attributes.Add("c_dt", c_dt); 
            ((LinkButton)e.Item.FindControl("LbtDel")).Attributes.Add("caid", caid);
            ((LinkButton)e.Item.FindControl("LbtDel")).Attributes.Add("onclick", "javascript:return confirm('您确认删除当前事务吗？')");
        }
    }

    protected void LbtEdit_Click(object sender, EventArgs e)
    {
        int caid = int.Parse(((LinkButton)sender).Attributes["caid"]);
        string c_dt = ((LinkButton)sender).Attributes["c_dt"]; 
        Response.Redirect("CalendarEdit.aspx?ca_id=" + caid + "&c_dt=" + c_dt);
    }

    protected void LbtDel_Click(object sender, EventArgs e)
    {
        string txtStart = Request.Form.Get(this.TxtStart.ClientID);
        string txtEnd = Request.Form.Get(this.TxtEnd.ClientID);
        string type = Request.Form.GetValues(this.selectCalType.ClientID)[0]; 
        int caid = int.Parse(((LinkButton)sender).Attributes["caid"]);
        UserCalendarArrangetbService ucatbService = new UserCalendarArrangetbService();
        ucatbService.delCalendarArrangeInfoById(caid);
        this.DataList_DataTable(txtStart, txtEnd, type);
    }

    protected void LbtDisplay_Click(object sender, EventArgs e)
    {
        int caid = int.Parse(((LinkButton)sender).Attributes["caid"]);
        string c_dt = ((LinkButton)sender).Attributes["c_dt"]; 
        Response.Redirect("CalendarEdit.aspx?ca_id=" + caid + "&c_dt=" + c_dt);
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        string txtStart = Request.Form.Get(this.TxtStart.ClientID);
        string txtEnd = Request.Form.Get(this.TxtEnd.ClientID);
        string type = Request.Form.GetValues(this.selectCalType.ClientID)[0]; 
        if ("".Equals(txtStart) || "".Equals(txtEnd))
        {
            ScriptManager.RegisterClientScriptBlock(Page, GetType(), "", "alert('日期填写不完整！');", true);
        }
        else
        {
            this.DataList_DataTable(txtStart, txtEnd, type);
        } 
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        string c_dt = null ;
        if (Request.QueryString["c_dt"] != null)
        {
            c_dt = Request.QueryString["c_dt"];
            this.HyperLink2.NavigateUrl = "CalendarArrangeShow.aspx?c_dt=" + c_dt;
        }
        else
        { 
            c_dt = DateTime.Now.ToShortDateString().Replace("-", "/");
            this.HyperLink2.NavigateUrl = "CalendarArrangeShow.aspx?c_dt=" + c_dt;
        }
        
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>无标题页</title>
       <link rel="Stylesheet" type="text/css" href="../Styles/right_style.css" />
       <link rel="Stylesheet" type="text/css" href="../Scripts/jquery.ui/css/start/jquery-ui.css" />

   <script type="text/javascript" src="../Scripts/jquery.ui/js/jquery.js"></script>
   <script type="text/javascript" src="../Scripts/jquery.ui/js/jquery-ui.js"></script>
   <script type="text/javascript" src="../Scripts/My97DatePicker/WdatePicker.js"></script>

</head>
<body>
    <form id="form1" runat="server">
     <div  class="sub_right_top">
            <!--滑动门-->
    <div class="hdm_header" >
     <div class="tb1_off" id="tab1"><h2 class="h2_icon">
     <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/CalendarArrange/ArrangeIndex.aspx">日程安排</asp:HyperLink>
     </h2></div>
     <div class="tb1_off" id="tab2"><h2 class="h2_icon">
      <asp:HyperLink ID="HyperLink2" runat="server">今日日程</asp:HyperLink>
     </h2></div>
      <div class="tb1_on" id="tab3"><h2 class="h2_icon">
            <asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl="~/CalendarArrange/CalendarArrangeDemand.aspx">日程查询</asp:HyperLink>
      </h2></div>
     <div class="clear"></div>
   </div>
   <div class="hdm_footer">
   <br />
   <table width="99%" border="1" align="center" cellpadding="0" cellspacing="0"  bordercolor="#77acd0"style="border-collapse:collapse; ">
          <tr bgcolor="#e4f1fd">
            <td colspan="2" align="center"><strong>日程查询信息</strong></td>
          </tr>
          <tr>
            <td width="100">日期：</td>

            <td><label>
               <asp:TextBox ID="TxtStart" runat="server"  CssClass="Wdate" onClick="WdatePicker()"></asp:TextBox>
            </label>
              至
              <label>
              <asp:TextBox ID="TxtEnd" runat="server"  CssClass="Wdate" onClick="WdatePicker()"></asp:TextBox>
              </label></td>
          </tr>
          <tr>

            <td width="100">事务类型：</td>
            <td><label>
              <select id="selectCalType" class="BigSelect" name="selectCalType" runat="server" style="width: 150px;">
                        <option selected="selected" value="">所有</option>
                        <option value="1">工作事务</option>
                        <option value="2">个人事务</option>
                    </select>
            </label></td>
          </tr>
          <tr>
            <td width="100">&nbsp;</td>
            <td><div class="disk_queding_top"><asp:Button ID="Button1" runat="server" Text="查询" Width="60px" OnClick="Button1_Click" />&nbsp;
                    </div>
            <div class="disk_queding_top"><asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="返回" Width="60px" Height="24px" /></div></td>
          </tr>
        </table>
        <br />

             <asp:Repeater ID="DataList1" runat="server" OnItemDataBound="DataList1_ItemDataBound">
                <HeaderTemplate>
                <table width="99%" border="1" align="center" cellpadding="0" cellspacing="0" bordercolor="#77acd0" style="border-collapse:collapse; ">
                    <tr align="center" style=" font-weight: bold;">
                        <td style="width: 24%;">事务标题</td>
                        <td style="width: 8%;">开始时间</td>
                        <td style="width: 8%;">结束时间</td>
                        <td style="width: 10%;">事务类型</td>
                        <td style="width: 25%;">事务内容</td>
                        <td style="width: 10%;">状态</td>
                        <td style="width: 15%;">操作</td>
                    </tr>
            </HeaderTemplate>
             <ItemTemplate>
                    <tr>
                        <td>
                                <asp:Label ID="LblTitle" runat="server" Text='<%# Eval("CalendarArrangeTitle").ToString().Trim() %>'></asp:Label>
                        </td>
                        <td>
                                <asp:Label ID="LblStartTime" runat="server" Text='<%# Eval("CalendarArrangeStartTime") %>'
                                   ></asp:Label>
                        </td>
                        <td>
                                <asp:Label ID="LblEndTime" runat="server" Text='<%# Eval("CalendarArrangeEndTime") %>'
                                   ></asp:Label>
                        </td>
                        <td>
                                <asp:Label ID="LblType" runat="server" Text='<%# Eval("CalendarArrangeType") %>'
                                    ></asp:Label>
                        </td>
                        <td>
                                <asp:Label ID="LblContent" runat="server" Text='<%# Eval("CalendarArrangeContent").ToString().Length > 20 ? Eval("CalendarArrangeContent").ToString().Trim().Substring(0,20) + "..." : Eval("CalendarArrangeContent").ToString().Trim() %>'
                                   ></asp:Label>
                        </td>
                        <td >
                                <asp:Label ID="LblState" runat="server" Text='<%# Eval("CalendarArrangeState") %>'
                                    ></asp:Label>
                                    <input id="Hidcaid" runat="server" style="width: 66px" type="hidden" value='<%# Eval("CalendarArrangeId") %>' />
                                     <input id="HidCalendarDate" runat="server" style="width: 49px" type="hidden" value='<%# Eval("CalendarArrangeDate") %>' />
                        </td>
                        <td>
                            <asp:LinkButton ID="LbtDisplay" runat="server"  Font-Underline="False" ForeColor="Teal" OnClick="LbtDisplay_Click"><font style="font-size:12px">查看</font></asp:LinkButton>
                            <asp:LinkButton ID="LbtEdit" runat="server"
                                Font-Underline="False" ForeColor="Teal" OnClick="LbtEdit_Click"><font style="font-size:12px">编辑</font></asp:LinkButton>
                            <asp:LinkButton ID="LbtDel" runat="server"  Font-Size="Smaller"
                                Font-Underline="False" ForeColor="Teal" OnClick="LbtDel_Click"><font style="font-size:12px">删除</font></asp:LinkButton></td>
                    </tr>
                
            </ItemTemplate>
            <FooterTemplate>
                </table>
            </FooterTemplate>
            </asp:Repeater>
            </div>
        </div>
    </form>
</body>
</html>
